<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 测评管理
 *
 * @icon fa fa-circle-o
 */
class Project extends Backend
{

    /**
     * Project模型对象
     * @var \app\common\model\Project
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Project;
        $this->view->assign("recommendswitchList", $this->model->getRecommendswitchList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $row) {
                $row->getRelation('category')->visible(['name']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * Account:删除项目
     * User: Ixiangang
     * Date: 2025/1/18
     * Time: 14:40
     */
    public function del($ids = null)
    {
        if (false === $this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ?: $this->request->post("ids");
        if (empty($ids)) {
            $this->error(__('Parameter %s can not be empty', 'ids'));
        }
        //删除题目
        \app\common\model\Question::where('project_id', $ids)->delete();
        //删除题库选项
        \app\common\model\Options::where('project_id', $ids)->delete();
        // 删除答案
        \app\common\model\Grading::where('project_id', $ids)->delete();
        //删除对应答的题目
        \app\common\model\UserAnswers::where('project_id', $ids)->delete();
        //删除测评报告
        \app\common\model\UserRecord::where('project_id', $ids)->delete();
        # 删除项目
        $this->model->where(['id'=>['in', $ids]])->delete();
        $this->success(__('Success'));
    }



}
